using System;
using System.Linq;
using System.Drawing.Drawing2D;
using System.Data;
using System.Data.Odbc;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Reviewer.Business;
using Reviewer.Entities.Services;
using Reviewer.Entities;

public partial class V2Controls_Top10ameldelser : System.Web.UI.UserControl
{
    private delegate Review[] LoadReviewsDelegate();
    private LoadReviewsDelegate loadHandler;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.AddOnPreRenderCompleteAsync(
                new BeginEventHandler(this.BeginAsyncLoad),
                new EndEventHandler(this.EndAsyncLoad)
                );
        }
    }

    private IAsyncResult BeginAsyncLoad(object sender, EventArgs e, AsyncCallback cb, object state)
    {
        this.loadHandler = new LoadReviewsDelegate(LoadReviews);
        return this.loadHandler.BeginInvoke(cb, state);
    }

    private void EndAsyncLoad(IAsyncResult ar)
    {
        this.uiRepReviews.DataSource = this.loadHandler.EndInvoke(ar);
        this.uiRepReviews.DataBind();
    }

    private Review[] LoadReviews()
    {
        using (IReviewService reviewService = ServiceFactory.ReviewService)
        {
            return reviewService.Find().OrderByDescending(t => t.Id).Take(10).ToArray();
        }
    }

    protected void uiRepReviews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Review review = e.Item.DataItem as Review;

            HyperLink link = e.Item.FindControl("uiHlReview") as HyperLink;
            Literal title = e.Item.FindControl("uiLitTitle") as Literal;
            Literal format = e.Item.FindControl("uiLitFormat") as Literal;
            Literal date = e.Item.FindControl("uiLitDate") as Literal;

            using (IGameService gameService = ServiceFactory.GameService)
            using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
            {
                Format currentFormat = null;
                Game currentGame = gameService.GetBy(review.GameId);
                if (currentGame != null && currentGame.Format > 0)
                {
                    currentFormat = formatService.GetAll().FirstOrDefault(t => t.Id.Equals(currentGame.Format));
                }
                else
                {
                    e.Item.Visible = false;
                    return;
                }

                link.NavigateUrl = UrlUtillity.GetReviewUri(review);

                title.Text = (currentGame.Title.Length > 30) ? currentGame.Title.Substring(0, 30) + "..." : currentGame.Title;
                format.Text = (currentFormat == null) ? "Ukendt format" : currentFormat.Name;
                date.Text = review.LastModifiedDate.ToLongDateString();
            }
        }
    }
}